package com.webull.information.center.carwler.common.util.news;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.webull.framework.util.UtilDate;
import com.webull.information.center.carwler.common.util.jsoup.HttpJsoupClient;
import com.webull.information.center.carwler.common.util.jsoup.JsoupModel;
import com.webull.information.client.model.News;

/**
 * 彭博topNews采集
 * 
 * @author smj
 * @Date 2017年8月17日下午6:04:06
 * @version 1.0
 *
 */
@Component
public class BloombergTopNewsUitl {
	private @Autowired
    HttpJsoupClient htmlPrase;
	protected final Logger logger = LogManager.getLogger(getClass());

	/*
	 * 获取CNBC个股资讯首页对象并解析新闻条目列表
	 */
	public List<BloomergItem> parseTopNewsList() {// url="https://www.bloomberg.com/quote/AAPL:US"
		String url = "https://www.bloomberg.com/markets";
		Document doc = htmlPrase.parseDocumentFromUrl(url, JsoupModel.PC0, true);
		if (doc == null) {
			logger.warn("BloombergTopNewsUitl parseNewsList parse url:{} ,is null!!", url);
			return null;
		}
		List<BloomergItem> result = new ArrayList<>();
		try {
			List<BloomergItem> newsList = parseTopNewsList(doc);
			if(newsList!=null && !newsList.isEmpty())
				result.addAll(newsList);
		} catch (Exception e) {
			logger.warn("BloombergTopNewsUitl parseTopNewsList parse url:{} ,is null!!", url);
		}
		try {
			List<BloomergItem> newsList = parseFrontNewsList(doc);
			if (newsList != null && !newsList.isEmpty())
				result.addAll(newsList);
		} catch (Exception e) {
			logger.warn("BloombergTopNewsUitl parseFrontNewsList parse url:{} ,is null!!", url);
		}
		return result;
	}

	private List<BloomergItem> parseFrontNewsList(Document doc) {
		List<BloomergItem> result = new ArrayList<>();

		Optional.ofNullable(doc.select("div.for-hero-big-story-image-height").first()).ifPresent(fg0 -> {
			BloomergItem item = new BloomergItem();
			item.setHome(News.HomeType.frontPage);

			Optional.ofNullable(fg0.select("a.hero-v2-big-story__image").first()).ifPresent(a0 -> {
				item.setLink(a0.absUrl("href"));
				item.setPicutre(getPicFromStyle(a0.attr("style")));
			});
			Optional.ofNullable(fg0.select("a[data-resource-type=article]").first()).ifPresent(a0 -> {
				item.setTitle(a0.text());
			});

			item.setNewsTime(getBBTime(fg0));
			if (item.check())
				result.add(item);
		});

		/*
		 * top two
		 */
		Optional.ofNullable(doc.select("div.two-up>div>article")).ifPresent(tws -> {
			tws.forEach(tw0 -> {
				BloomergItem item = new BloomergItem();
				item.setHome(News.HomeType.frontPage);

				Optional.ofNullable(tw0.select("a.two-up-story__headline-link").first()).ifPresent(a0 -> {
					item.setTitle(a0.text());
				});
				Optional.ofNullable(tw0.select("a.two-up-story__image").first()).ifPresent(a0 -> {
					item.setLink(a0.absUrl("href"));
					item.setPicutre(getPicFromStyle(a0.attr("style")));
				});

				item.setNewsTime(getBBTime(item.getLink()));
				if (item.check())
					result.add(item);
			});
		});

		/*
		 * top three
		 */
		Optional.ofNullable(doc.select("div.three-up>div>article")).ifPresent(tws -> {
			tws.forEach(tw0 -> {
				BloomergItem item = new BloomergItem();
				// item.setHome(News.HomeType.frontPage);

				Optional.ofNullable(tw0.select("a.three-up-story__headline-link").first()).ifPresent(a0 -> {
					item.setTitle(a0.text());
				});
				Optional.ofNullable(tw0.select("a.three-up-story__image").first()).ifPresent(a0 -> {
					item.setLink(a0.absUrl("href"));
					item.setPicutre(getPicFromStyle(a0.attr("style")));
				});
				item.setNewsTime(getBBTime(tw0));
				if (item.check())
					result.add(item);
			});
		});
		return result;

	}
	private List<BloomergItem> parseTopNewsList(Document doc) {
		List<BloomergItem> result = new ArrayList<>();

		// 美股新闻页面
		Optional.ofNullable(doc.select("section.top-news-v3>ul.top-news-v3__stories>li"))//
				.ifPresent(v -> {
					v.forEach(v0 -> {
						Element tag = v0.getElementsByTag("article").first();
						if (tag == null)
							return;

							BloomergItem item = new BloomergItem();
							Optional.ofNullable(tag.select("h1>a").first()).ifPresent(a0 -> {
								item.setLink(a0.absUrl("href"));
								item.setTitle(a0.text());
							}); 
							item.setNewsTime(getBBTime(tag));
							item.setPicutre(getBBimg(item.getLink()));
							if (item.check())
							result.add(item);
					});
				});

		return result;
	}

	/*
	 * 主图需要模拟手机端进入详情页，后在获取
	 */
	private String getBBimg(String itemUrl) {
		Document doc = htmlPrase.parseDocumentFromUrl(itemUrl, JsoupModel.Mobile0, true);
		// Document doc = get(itemUrl);
		if (doc == null)
			return null;
		return Optional.ofNullable(doc.select("div.lede-large-image__image").first())
				.map(v -> getPicFromStyle(v.attr("style"))).orElse(null);
	}

	private Date getBBTime(Element tag) {
	 String date = Optional.ofNullable(tag.select("time.published-at").first())//
				.map(t0 -> StringUtils.stripToNull(t0.attr("datetime"))).orElse(null);//
			
		if(date!=null)		
		try {
				return UtilDate.parse(date, 0, "yyyy-MM-dd'T'HH:mm:ss");
		}catch (Exception e) {
			logger.warn("BloombergTopNewsUitl error", e);
		}
		return null;
	}
	/*
	 * 从详情页获取新闻时间
	 */
	private Date getBBTime(String itemUrl) {
		Document doc = htmlPrase.parseDocumentFromUrl(itemUrl, JsoupModel.Mobile0, true);
		// Document doc = get(itemUrl);
		if (doc == null)
			return null;
		String string = Optional.ofNullable(doc.select("time.article-timestamp").first()).map(v -> v.attr("datetime"))
				.orElse(null);
		if (string != null) {
			try {
				return UtilDate.parse(string, 0, "yyyy-MM-dd'T'HH:mm:ss");
			} catch (Exception e) {
				logger.warn("BloombergTopNewsUitl time error:url:{} error", itemUrl, e);
			}
		}
		return null;
	}

	private String getPicFromStyle(String style) {
		return Optional.ofNullable(StringUtils.substringBetween(style, "(", ")")).filter(v -> v.length() > 2)
				.map(v -> StringUtils.strip(v, "'")).orElse(null);
	}

	public static void main(String[] args) throws IOException {

		String url2 = "https://www.bloomberg.com/markets";

		// Connection connection = Jsoup.connect(url2)
		// Connection connection = Jsoup.connect(urlHK)
		Connection connection = Jsoup.connect(url2).userAgent(
				"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36")
				// .header("x-client-data",
				// "CIq2yQEIpbbJAQjEtskBCP2VygEIwpjKAQjwnMoB")
				.header("x-client-data", RandomStringUtils.randomAlphanumeric(40));
//		connection.proxy("10.1.2.174", 3088);
		connection.proxy("127.0.0.1", 1080);

		org.jsoup.nodes.Document doc = connection.timeout(10000).get();

		// List<CNBCItem> items = new
		// BloombergNewsListUitl().parseNewsListHK(doc);
		List<BloomergItem> items = new BloombergTopNewsUitl().parseFrontNewsList(doc);
		System.out.println(items);
		items = new BloombergTopNewsUitl().parseTopNewsList(doc);
		for (int i = 0; i < items.size(); i++) {
			System.out.println(items.get(i).getLink());
		}
		System.out.println(items);
	}

	private static Document get2(String url) {
		Connection connection = Jsoup.connect(url).userAgent(
				"Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1")
				.header("cache-control", "no-cache").header("x-ivanka-platform", "wscn-platform");
		connection.proxy("127.0.0.1", 1080);
		Document doc2 = null;
		try {
			doc2 = connection.timeout(10000).get();
		} catch (IOException e) {
		}
		return doc2;
	}
	public static class BloomergItem {
		private Date newsTime;
		private Integer home = News.HomeType.top;
		private String title;
		private String link;
		private String picutre;
		private boolean check() {
			if (newsTime == null)
				return false;
			if (StringUtils.isBlank(title))
				return false;
			if (StringUtils.isBlank(link))
				return false;
			return true;
		}
		public Date getNewsTime() {
			return newsTime;
		}

		public void setNewsTime(Date newsTime) {
			this.newsTime = newsTime;
		}

		public String getTitle() {
			return title;
		}

		public void setTitle(String title) {
			this.title = title;
		}

		public String getLink() {
			return link;
		}

		public void setLink(String link) {
			this.link = link;
		}



		public String getPicutre() {
			return picutre;
		}

		public void setPicutre(String picutre) {
			this.picutre = picutre;
		}
		public Integer getHome() {
			return home;
		}

		public void setHome(Integer home) {
			this.home = home;
		}
		@Override
		public String toString() {
			return "BloomergItem [newsTime=" + newsTime + ", title=" + title + ", link=" + link
					+ "]";
		}

	}

}
